/*
 * @(#) IndexNews.java 1.0  2011-8-22 
 *
 * (c) Copyright 2011 smshen S3H3L3
 *
 * FILENAME    : IndexNews.java
 * PACKAGE     : com.sm.lucene
 * CREATE DATE : 2011-8-22
 * AUTHOR      : smshen
 */

package com.sm.lucene;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

import com.sm.model.News;
import com.sm.service.NewsService;

/*******************************************************************************
 * #(c) smshen S3H3L3
 * 
 * 功能说明:创建新闻索引
 * 
 * 2011-8-22 上午10:11:01 smshen 创建文件
 * 
 * 修改说明: 创建文件
 * 
 * 2011-8-22 上午10:11:01 smshen 修改文件
 * 
 ******************************************************************************/

public class IndexNews {

	/**
	 * newsService 新闻管理服务
	 */
	private NewsService newsService;

	/**
	 * 为newsService初始化
	 * 
	 * @param newsService
	 *            新闻管理服务
	 */
	public IndexNews(NewsService newsService) {
		this.newsService = newsService;
	}

	/**
	 * 
	 * 功能 :创建索引
	 * 
	 * 开发：smshen 2011-8-23
	 */
	@SuppressWarnings("deprecation")
	public void createNewsIndex() {
		// 索引的存储路径
		File indexDir = new File(System.getProperty("user.dir") + "\\workspace\\S3H3L3\\index");
//		File indexDir = new File("S3H3L3\\index");
		IndexWriter indexWriter = null;
		try {
			// 创建Directory对象
			Directory dir = new SimpleFSDirectory(indexDir);
			// 创建IndexWriter对象,第一个参数是Directory,第二个是分词器,第三个表示是否是创建,如果为false为在此基础上面修改,第四表示表示分词的最大值，比如说new
			// MaxFieldLength(2)，就表示两个字一分，一般用IndexWriter.MaxFieldLength.LIMITED
			indexWriter = new IndexWriter(dir, new StandardAnalyzer(
					Version.LUCENE_30), true,
					IndexWriter.MaxFieldLength.UNLIMITED);
			Iterator<News> it = newsService.getAllNews().iterator();
			Document doc;
			News news = new News();
			while (it.hasNext()) {
				news = it.next();
				doc = new Document();
				doc.add(new Field("id", news.getId() + "", Field.Store.YES,
						Field.Index.NOT_ANALYZED));
				doc.add(new Field("title", news.getTitle() + "",
						Field.Store.YES, Field.Index.ANALYZED));
				doc.add(new Field("author", news.getAuthor() + "",
						Field.Store.YES, Field.Index.ANALYZED));
				doc.add(new Field("content", news.getContent(),
						Field.Store.YES, Field.Index.ANALYZED));
				doc.add(new Field("digest", news.getDigest() + "",
						Field.Store.YES, Field.Index.ANALYZED));
				doc.add(new Field("keyword", news.getKeyword() + "",
						Field.Store.YES, Field.Index.ANALYZED));
				doc.add(new Field("time", news.getTime() + "", Field.Store.YES,
						Field.Index.ANALYZED));
				indexWriter.addDocument(doc);
			}
			System.out.println("创建索引" + indexWriter.numDocs() + "条");
			System.out.println("索引存储目录:" + indexDir);
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				indexWriter.optimize();
				indexWriter.close();
			} catch (CorruptIndexException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
